﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Fdd.Aspen.Infrastructure.Data;
using System.Configuration;
using Fdd.Aspen.Repositories;
using Fdd.Aspen.DomainModel.Repositories;
using System.ComponentModel.Composition;
using Fdd.Aspen.DomainModel.Entities;
using System.Transactions;
using WcfRiaServices.Extensions;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using System.Diagnostics.Contracts;
using Fdd.Aspen.Services.Web.PresentationModel;
using Fdd.Aspen.DomainModel.Repositories.Interfaces;

namespace Fdd.Aspen.Services.Web
{
    [EnableClientAccess()]
    public class MemberDomainService : ObjectContextDomainService
    {
        readonly IMemberRepository _memberRepository = null;
        readonly IGatheringRepository _gatheringRepository = null;


        public MemberDomainService(
                                   IMemberRepository membershipRepository,
                                   IGatheringRepository gatheringRepository)
        {
            Contract.Requires(membershipRepository != null);
            Contract.Requires(gatheringRepository != null);
            Contract.Ensures(_memberRepository != null);
            Contract.Ensures(_gatheringRepository != null);

            _memberRepository = membershipRepository;
            _gatheringRepository = gatheringRepository;
        }


        public void UpdateMember(MemberPM member)
        {
            Contract.Requires(member != null);

            var memberToUpdate = _memberRepository.Single(m => m.MemberId == member.MemberId);

            memberToUpdate.FirstName = member.FirstName;
            memberToUpdate.LastName = member.LastName;
        }


        public void AddMember(MemberPM member)
        {
            Contract.Requires(member != null);

            _memberRepository.Add(new Member(member.FirstName, member.LastName)
                                    {
                                        MemberDate = DateTime.Now
                                    });
        }


        public void RemoveMember(MemberPM member)
        {
            Contract.Requires(member != null);

            _memberRepository.Delete(new Member(member.FirstName, member.LastName)
                                    {
                                        MemberId = member.MemberId
                                    });
        }


        public IEnumerable<MemberPM> GetMembers()
        {
            Contract.Ensures(Contract.Result<IEnumerable<MemberPM>>() != null);

            return _memberRepository.GetAll()
                                    .Select(m => new MemberPM()
                                                    {
                                                        MemberId = m.MemberId,
                                                        FirstName = m.FirstName,
                                                        LastName = m.LastName
                                                    });
        }


        [ContractInvariantMethod]
        private void ObjectInvariant()
        {
            Contract.Invariant(_gatheringRepository != null);
            Contract.Invariant(_memberRepository != null);
        }
    }
}


